package de.tu_darmstadt.gdi.battleship;

import java.util.Vector;

public class Battleground {

	public Vector<Ship> playGround;

	/**
	 * Standard constructor, which creats form a map a battleground.
	 * 
	 * @param map
	 *            A instance of the map class.
	 * @throws Exception
	 */
	public Battleground(Map map) throws Exception {
		playGround = new Vector<Ship>();
		char[][] temp = map.returnWholeMap();
		char[][] temp2 = new char[10][21];
		
		for(int i = 0; i < 10; i++){
			for(int j = 0; j < 21; j++){
				temp2[i][j] = temp[j][i];
			}
		}
		temp = temp2;
		
		for (int i = 0; i < temp.length; i++) {
			for (int j = 0; j < temp[i].length; j++) {
				char field = temp[i][j];
				switch (field) {
				case 't':
					// Prüfe ob wir nicht die Arraygrenzen verletzten
					if (i + 1 < 10)
						// Prüfe, ob das nächste Feld ein b ist, sprich das
						// letzte Element des Schiffes.
						if (temp[i + 1][j] == 'b') {
							int[] koords = new int[2];
							koords[0] = i;
							koords[1] = j;
							int[] endkoords = new int[2];
							endkoords[0] = i + 1;
							endkoords[1] = j;
							playGround.add(new Speedboat(koords, endkoords));
						}
					if (i + 2 < 10)
						if (i + 2 < 10 && temp[i + 2][j] == 'b') {
							int[] koords = new int[2];
							koords[0] = i;
							koords[1] = j;
							int[] endkoords = new int[2];
							endkoords[0] = i + 2;
							endkoords[1] = j;
							playGround.add(new Destroyer(koords, endkoords));
						}
					if (i + 3 < 10)
						if (i + 3 < 10 && temp[i + 3][j] == 'b') {
							int[] koords = new int[2];
							koords[0] = i;
							koords[1] = j;
							int[] endkoords = new int[2];
							endkoords[0] = i + 3;
							endkoords[1] = j;
							playGround.add(new Cruiser(koords, endkoords));
						}
					if (i + 4 < 10)
						if (i + 4 < 10 && temp[i + 4][j] == 'b') {
							int[] koords = new int[2];
							koords[0] = i;
							koords[1] = j;
							int[] endkoords = new int[2];
							endkoords[0] = i + 4;
							endkoords[1] = j;
							playGround.add(new Battleship(koords, endkoords));
						}
					break;
				case 'l':
					if (j + 1 < 21)
						if (temp[i][j + 1] == 'r') {
							int[] koords = new int[2];
							koords[0] = i;
							koords[1] = j;
							int[] endkoords = new int[2];
							endkoords[0] = i;
							endkoords[1] = j + 1;
							Speedboat tempBoat = new Speedboat(koords,
									endkoords);
							playGround.add(tempBoat);
						}
					if (j + 2 < 21)
						if (temp[i][j + 2] == 'r') {
							int[] koords = new int[2];
							koords[0] = i;
							koords[1] = j;
							int[] endkoords = new int[2];
							endkoords[0] = i;
							endkoords[1] = j + 2;
							playGround.add(new Destroyer(koords, endkoords));
						}
					if (j + 3 < 21)
						if (temp[i][j + 3] == 'r') {
							int[] koords = new int[2];
							koords[0] = i;
							koords[1] = j;
							int[] endkoords = new int[2];
							endkoords[0] = i;
							endkoords[1] = j + 3;
							playGround.add(new Cruiser(koords, endkoords));
						}
					if (j + 4 < 21)
						if (temp[i][j + 4] == 'r') {
							int[] koords = new int[2];
							koords[0] = i;
							koords[1] = j;
							int[] endkoords = new int[2];
							endkoords[0] = i;
							endkoords[1] = j + 4;
							playGround.add(new Battleship(koords, endkoords));
						}
					break;
				case '-':
					int[] coords = new int[2];
					coords[0] = i;
					coords[1] = j;
					playGround.add(new Water(coords, coords));
					break;
				}
			}
		}
	}

	/**
	 * This method returns the actual field with its ships
	 * 
	 * @return Vector<Ship> The actuall playGround with all ships and there
	 *         status i.e
	 */
	public Vector<Ship> returnPlayGround() {
		return this.playGround;
	}

	/**
	 * This method returns the objects as a string, which is similar to the
	 * encoding of a level or save game.
	 */
	public String toString(){
		char[][] tempField = new char[10][21];
		// Das Array muss werte enthalten, damit es so funktioniert, wie ich es möchte.
		for(int i = 0; i < 10; i++){
			for(int j = 0; j < 21; j++){
				tempField[i][j] = '|';
			}
		}
		for(Ship ship: playGround){
			for(int i = 0; i < ship.sections.size(); i++){
				int startX = ship.start[0];
				int startY = ship.start[1];
				int endX = ship.end[0];
				int endY = ship.end[1];
				if(startX == endX){
					char symbol = ship.sections.get(i).getSymbol();
					tempField[ship.start[0]][ship.start[1]+i] = symbol;
				}else{
					char symbol = ship.sections.get(i).getSymbol();
					tempField[ship.start[0]+i][ship.start[1]] = ship.sections.get(i).getSymbol();
				}
			}
		}
		StringBuilder sb = new StringBuilder();
		for(int i = 0; i < 10; i++){
			for(int j = 0; j < 21; j++){
				sb.append(tempField[i][j]);
			}
			sb.append("\n");
		}
		return sb.toString();
	}
	
}
